package com.ken.blog.filter;

import com.ken.blog.component.CategoryComponent;
import com.ken.blog.domain.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Component
public class WebFilter implements HandlerInterceptor {

    @Autowired
    private CategoryComponent categoryComponent;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handle) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handle, ModelAndView modelAndView) throws Exception {
        //查询所有分类
        List<Category> categoryList = categoryComponent.getCategoryList();
        modelAndView.addObject("categoryList", categoryList);

        modelAndView.addObject("siteName", "Spring博客系统学习");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex) throws Exception {
    }
}
